import {default as gc, req} from './gc'

const RESTAPI_ADDR = process.env.RESTAPI_ADDR

export const fetch = async ({query, variables = {}, cache = true}) => {
  try {
    cache = false
    let res = null
    let rs = null
    res = await gc({
      query,
      variables,
      cache
    })
    rs = res
    return rs
  } catch (e) {
    console.error(e)
    throw e
  }
}

export const get = async ({path, data = null, header = null}) => {
  try {
    let res = null
    let rs = null
    rs = await req({
      url: `${RESTAPI_ADDR}${path}`,
      data,
      header,
      method: 'GET',
    })
    return rs.data
  } catch (e) {
    console.error(e)
    throw e
  }
}

export const post = async ({path, data = null, header = null}) => {
  try {
    let res = null
    let rs = null
    rs = await req({
      url: `${RESTAPI_ADDR}${path}`,
      data,
      header,
      method: 'POST',
    })
    return rs.data
  } catch (e) {
    console.error(e)
    throw e
  }
}

export default {
  fetch,
}
